<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\Admin\ActivityComRedPacket\CreateRequest;
use App\Http\Requests\Admin\ActivityComRedPacket\UpdateRequest;
use App\Models\Admin\ActivityComRedPacket;
use Illuminate\Http\Request;
use Style;

class ActivityComRedPacketController extends CommonController
{
    /**
     * 红包列表页
     * @return mixed
     */
    public function index()
    {
        $red_packet = ActivityComRedPacket::select('id','vip_post_number','vip_resume_number','vip_integral','resume_number','integral',
            'viptime','frequency','time_interval','end_at')
            ->orderBy('created_at','asc')
            ->paginate(20);
        return Style::view('admin.toll.activity_com_red_packet.index',compact('red_packet'));
    }

    /**
     * 红包创建
     * @return mixed
     */
    public function create()
    {
        return Style::view('admin.toll.activity_com_red_packet.create');
    }

    /**
     * 红包创建上传
     * @param CreateRequest $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(CreateRequest $request)
    {
        $input = $request->only(['vip_post_number','vip_resume_number','vip_integral','resume_number','integral','viptime',
                                'frequency','time_interval','end_at','description']);
        $input['pic_url'] = '';
        if ($request->hasFile('pic_url')){
            foreach ($request->file('pic_url') as $v){
                $extension = strtolower($v -> getClientOriginalExtension());
                 //判断文件上传大小
                if($v ->getClientSize() > $v ->getMaxFilesize() ){
                    return  back()->with('errors','文件大小最大只能上传2M！');
                }
                //判断是不是一个有效的文件
                if ($v->isValid()) {
                    if (!in_array($extension, array('gif','jpg','jpeg','png'))){
                        return  back()->with('errors','文件类型只能为 png, jpeg , jpg 或 gif的图片！');
                    }else{
                        $destinationPath = '/upload/redPackeg/'; //后台上传文件广告图片位置
                        $fileurl=date("Ym").'/';
                        $filename=time().strtolower(str_random(5)).'.'.$extension;//定义文件名
                        $v->move(public_path().$destinationPath.    $fileurl,$filename);//把缓存文件移动到指定文件夹下
                        $input['pic_url'] .= $fileurl.$filename.',';
                    }
                }
            }
        }else{
            return  back()->with('errors','您上传的文件不存在');
        }
        $input['pic_url'] = trim($input['pic_url'],',');
        $input['created_at'] = date("Y-m-d H:i:s");
        $result = ActivityComRedPacket::create($input);
        if (count($result)>0){
            return redirect('admin/activity_com_red_packet');
        }
        return back()->with('errors',"红包添加失败，请稍后重试！");
    }

    /**
     * 红包修改
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $redPacket = ActivityComRedPacket::where('id',$id)->first();
        return Style::view('admin.toll.activity_com_red_packet.edit',compact('redPacket'));
    }

    /**
     * 更新修改
     * @param UpdateRequest $request
     * @param $id
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function update(UpdateRequest $request ,$id)
    {
        $input = $request->only(['vip_post_number','vip_resume_number','vip_integral','resume_number','integral','viptime',
            'frequency','time_interval','end_at','description']);
        if ($request->hasFile('pic_url')){
            //更新红包  如果重新选择了图片  先删除原先图片
            $pic_url = ActivityComRedPacket::where('id',$id)->first();
            if (!empty($pic_url->pic_url)){
                $picArr = explode(',',$pic_url->pic_url);
                foreach ($picArr as $value){
                    @unlink(public_path() . '/upload/redPackeg/'.$value);
                }
            }
            //再上传新图片
            $input['pic_url'] = '';
            foreach ($request->file('pic_url') as $v){
                $extension = strtolower($v -> getClientOriginalExtension());
                //判断文件上传大小
                if($v ->getClientSize() > $v ->getMaxFilesize() ){
                    return  back()->with('errors','文件大小最大只能上传2M！');
                }
                //判断是不是一个有效的文件
                if ($v->isValid()) {
                    if (!in_array($extension, array('gif','jpg','jpeg','png'))){
                        return  back()->with('errors','文件类型只能为 png, jpeg , jpg 或 gif的图片！');
                    }else{
                        $destinationPath = '/upload/redPackeg/'; //后台上传文件广告图片位置
                        $fileurl=date("Ym").'/';
                        $filename=time().strtolower(str_random(5)).'.'.$extension;//定义文件名
                        $v->move(public_path().$destinationPath.$fileurl,$filename);//把缓存文件移动到指定文件夹下
                        $input['pic_url'] .=$fileurl.$filename.',';
                    }
                }
            }
            $input['pic_url'] = trim($input['pic_url'],',');
        }
        $input['updated_at'] = date("Y-m-d H:i:s");
        $result = ActivityComRedPacket::where('id',$id)->update($input);
        if (count($result)>0){
            return redirect('admin/activity_com_red_packet');
        }
        return back()->with('errors',"红包修改失败，请稍后重试！");
    }

    /**
     * 删除红包
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     *
     */
    public function destroy($id)
    {
        $pic_url = ActivityComRedPacket::where('id',$id)->first();
        if (!empty($pic_url->pic_url)){
            $picArr = explode(',',$pic_url->pic_url);
            foreach ($picArr as $value){
                @unlink(public_path() . '/upload/redPackeg/'.$value);
            }
        }
        $result = ActivityComRedPacket::where('id',$id)->delete();
        if (count($result) > 0){
            return response()->json(['status'=>0,'msg'=>'红包删除成功！']);
        }
        return response()->json(['status'=>1,'msg'=>'红包删除失败，请稍后重试！']);
    }
}
